const Router = require('koa-router')

const router = new Router()

const multer = require('koa-multer') // 加载koa-multer模块
  // 上传图片
var storage = multer.diskStorage({
  // 文件保存路径
  destination: function(req, file, cb) {
    cb(null, './public/image/')
  },
  // 修改文件名称
  filename: function(req, file, cb) {
    var fileFormat = (file.originalname).split('.')
    cb(null, Date.now() + '.' + fileFormat[fileFormat.length - 1])
  }
})
// 加载配置
var upload = multer({
  storage: storage
})
router.post('/', upload.single('file'), async(ctx) => {
  ctx.body = {
    code: 101,
    data: {
      filename: ctx.req.file.filename // 返回文件名
    },
    msg: '成功'
  }
})

module.exports = router
